package nl.zoidberg.calculon.engine;

import java.util.List;

import nl.zoidberg.calculon.engine.BitBoard.BitBoardMove;
import nl.zoidberg.calculon.model.Piece;


public class RookMoveGenerator extends StraightMoveGenerator {

	public static int[][] DIRECTIONS = new int[][] {
		{ 1, 0 },
		{ 0, 1 },
		{ -1, 0 },
		{ 0, -1 },
	};

	@Override
	public int[][] getDirections() {
		return DIRECTIONS;
	}

	@Override
	public void generateMoves(BitBoard bitBoard, boolean alreadyInCheck, long potentialPins, List<BitBoardMove> rv) {
		byte player = bitBoard.getPlayer();
		
		long pieces = bitBoard.getBitmapColor(player) & bitBoard.getBitmapRooks();
		while(pieces != 0) {
			long nextPiece = Long.highestOneBit(pieces);
			pieces ^= nextPiece;
			boolean safeFromCheck = ((nextPiece & potentialPins) == 0) & !alreadyInCheck;
			
			int mapIdx = Long.numberOfTrailingZeros(nextPiece);
			makeUpBoardMoves(bitBoard, nextPiece, Bitmaps.maps2[Bitmaps.BM_U][mapIdx], 8, alreadyInCheck, safeFromCheck, rv);
			makeUpBoardMoves(bitBoard, nextPiece, Bitmaps.maps2[Bitmaps.BM_R][mapIdx], 1, alreadyInCheck, safeFromCheck, rv);
			makeDownBoardMoves(bitBoard, nextPiece, Bitmaps.maps2[Bitmaps.BM_L][mapIdx], 1, alreadyInCheck, safeFromCheck, rv);
			makeDownBoardMoves(bitBoard, nextPiece, Bitmaps.maps2[Bitmaps.BM_D][mapIdx], 8, alreadyInCheck, safeFromCheck, rv);
		}
	}

	@Override
	protected byte getPieceType() {
		return Piece.ROOK;
	}
}